#!/bin/bash

BC_SCALE=3
DATE=`date +"%y_%m_%d_%H_%M_%S"`
TMP_FILE=/tmp/temp_${DATE}_${RANDOM}

RUN_ALL_EXPERIMENTS_SCRIPT=../run_all_experiments.sh
READ_OUTPUT=./read_output.sh


FILES_COUNT=0

for file in ../*.cnf
do
	let FILES_COUNT++

done

BLOCKS=`cat $RUN_ALL_EXPERIMENTS_SCRIPT | egrep "^BLOCKS=" | cut -d "=" -f 2`

BLOCKS_COUNT=0

for block in $BLOCKS
do
	let BLOCKS_COUNT++
done

THREADS=`cat $RUN_ALL_EXPERIMENTS_SCRIPT | egrep "^THREADS=" | cut -d "=" -f 2`

THREADS_COUNT=0

for thread in $THREADS
do
	let THREADS_COUNT++
done

REPETITIONS=`cat $RUN_ALL_EXPERIMENTS_SCRIPT | egrep "^REPETITIONS=" | cut -d "=" -f 2`

let TOTAL=${FILES_COUNT}*${BLOCKS_COUNT}*${THREADS_COUNT}*${REPETITIONS}

TOTAL_RUN=0

for file in *.results
do

	$READ_OUTPUT $file > $TMP_FILE

	while read line
	do
		if [[ $line =~ "Count = "[0-9]+ ]]
		then
			COUNT=`echo $line | cut -d " " -f 3`
			let TOTAL_RUN=${TOTAL_RUN}+${COUNT}
		fi

	done < $TMP_FILE

done

PROGRESS=`echo "scale = $BC_SCALE; (${TOTAL_RUN}/${TOTAL})*100" | bc`

echo "The progress is ${PROGRESS}%"

rm $TMP_FILE
